using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Purchase;
using HengTong.Model;
using HengTong.Model._Admin;
using HengTong.Model.Db.SupplyChain.Material;


namespace HengTong.ViewModel.SupplyChain.SysPurchaseRequirementPoolVms
{
    public partial class SysPurchaseRequirementPoolListVm : BaseListVm<SysPurchaseRequirementPoolView, SysPurchaseRequirementPoolSearcher>
    {

        protected override IEnumerable<IGridColumn<SysPurchaseRequirementPoolView>> InitGridHeader()
        {
            return new List<GridColumn<SysPurchaseRequirementPoolView>>{
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.TotalAmount),
                this.MakeGridHeader(x => x.UrgentCount),
                this.MakeGridHeader(x => x.State),
                this.MakeGridHeader(x => x.IsDefault),
                this.MakeGridHeader(x => x.PurchaserInChargeName),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.MaterialGroupsName),
                this.MakeGridHeader(x => x.ManagersName),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysPurchaseRequirementPoolView> GetSearchQuery()
        {
            var query = DC.Set<SysPurchaseRequirementPool>()
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckEqual(Searcher.State, x=>x.State)
                .CheckEqual(Searcher.IsDefault, x=>x.IsDefault)
                .Select(x => new SysPurchaseRequirementPoolView
                {
				    ID = x.ID,
                    Name = x.Name,
                    Code = x.Code,
                    TotalAmount = x.TotalAmount,
                    UrgentCount = x.UrgentCount,
                    State = x.State,
                    IsDefault = x.IsDefault,
                    PurchaserInChargeName = x.PurchaserInCharge.Name,
                    Remark = x.Remark,
                    MaterialGroupsName = x.MaterialGroups.Select(y=>y.Group.Name).ToSepratedString(null,","), 
                    ManagersName = x.Managers.Select(y=>y.Manager.Name).ToSepratedString(null,","), 
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysPurchaseRequirementPoolView : SysPurchaseRequirementPool{
        [Display(Name = "_Admin.Name")]
        public String PurchaserInChargeName { get; set; }
        [Display(Name = "简称")]
        public String MaterialGroupsName { get; set; }
        [Display(Name = "_Admin.Name")]
        public String ManagersName { get; set; }

    }
}
